﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Shapes;
using Might.GraphicsEditor.GraphicsObjects;
using Might.GraphicsEditor.Presenter;
using Might.Infrastructure.Annotations.Graphics;

namespace Might.GraphicsEditor.Tools {
    ///<summary>
    ///</summary>
    public partial class RadiusRod {
        private bool IsMouseCaptured;
        private Point startPoint;
        internal IconPresenter graphics;

        internal RadiusRod(IconPresenter graphics) {
            InitializeComponent();

            this.graphics = graphics;
            topgrid.Visibility = Visibility.Collapsed;
            graphics.SelectionChanged += graphics_SelectionChanged;
            graphics.SelectionMoved += graphics_SelectionMoved;

            #region handlers

            rod.MouseLeftButtonDown += Rod_MouseLeftButtonDown;
            rod.MouseMove += Rod_MouseMove;
            rod.MouseLeftButtonUp += Rod_MouseLeftButtonUp;

            #endregion
        }

        #region Slider for changing CornerRadius

        internal double startRadius;

        internal void Rod_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
            // in case of flipping object, get actual handle
            startPoint = e.GetPosition(graphics.canvas);
            startRadius = ((LeafRectangle)graphics.SelectedShapes[0]).CornerRadius;

            ((Rectangle)sender).CaptureMouse();
            IsMouseCaptured = true;
            e.Handled = true;
        }

        /// <summary>
        /// Used when moving with one of resize handles.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        internal void Rod_MouseMove(object sender, MouseEventArgs e) {
            // proved switch podle toho, ktery handle drzim a posun podle toho ramecek
            if (IsMouseCaptured) {
                Point point = e.GetPosition(graphics.canvas);
                if (point.X < 0 || point.X > graphics.canvas.ActualWidth ||
                    point.Y < 0 || point.Y > graphics.canvas.ActualHeight) {
                    return;
                }

                double radius = startRadius + (point.X - startPoint.X);
                radius = Math.Max(radius, 0);
                radius = Math.Min(radius, Math.Max(graphics.SelectedShapes[0].Width, graphics.SelectedShapes[0].Height) / 2);

                ((IGraphicsRectangle)graphics.SelectedShapes[0].ga).CornerRadius = radius;
                UpdateRod();
            }
        }

        internal void Rod_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
            ((Shape)sender).ReleaseMouseCapture();
            IsMouseCaptured = false;
            e.Handled = true;
        }

        #endregion

        // This function is to be called after a block of operations, this also updates all the annotation values.
        internal void UpdateRod() {
            topgrid.Visibility = Visibility.Visible;
            double r = ((LeafRectangle)graphics.SelectedShapes[0]).CornerRadius;
            var t = new Thickness(r - 3, -25, 0, 0);
            rod.Margin = t;
            rodline.X1 = rodline.X2 = r;
            Canvas.SetLeft(this, graphics.SelectedShapes[0].Left);
            Canvas.SetTop(this, graphics.SelectedShapes[0].Top);
            //tahlocara.Y2 = r;
        }

        internal void graphics_SelectionChanged(object sender, EventArgs e) {
            if (graphics.SelectedShapes.Count == 1 && graphics.SelectedShapes[0] is LeafRectangle) UpdateRod();
            else topgrid.Visibility = Visibility.Collapsed;
        }

        private void graphics_SelectionMoved(object sender, EventArgs e) {
            if (graphics.SelectedShapes.Count == 1 && graphics.SelectedShapes[0] is LeafRectangle) UpdateRod();
            else topgrid.Visibility = Visibility.Collapsed;
        }
    }
}
